//
//  BTUDPViewController.m
//  BaseTools
//
//  Created by 李挺哲 on 15/9/1.
//  Copyright (c) 2015年 ltz. All rights reserved.
//

#import "BTUDPViewController.h"
#import "AsyncUdpSocket.h"
@interface BTUDPViewController (){

    	AsyncUdpSocket *udpSocket;
}
@property (weak, nonatomic) IBOutlet UITextField *ipText;
@property (weak, nonatomic) IBOutlet UITextField *portText;
@property (weak, nonatomic) IBOutlet UITextField *msgText;
@property (weak, nonatomic) IBOutlet UILabel *receiveMsgLabel;

@end

@implementation BTUDPViewController

- (void)initAsynUdp {
    udpSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
    
    NSError *error = nil;
    
    if (![udpSocket bindToPort:0 error:&error])
    {
        NSLog(@"Error binding: %@", error);
        return;
    }
    
    [udpSocket receiveWithTimeout:-1 tag:0];
    
    NSLog(@"Ready");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigationTitle:@"SOCKET(UDP)" barTintColor:0x1E86CB titleColor:0xFFFFFF isTranslucent:NO isHidden:NO];
    [self showNavigationLeftButtonTitle:@"返回" tintColor:0xFFFFFF];
    
    _ipText.text=@"127.0.0.1";
    _portText.text=@"9999";
    [self initAsynUdp];
    
//    udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
//    
//    NSError *error = nil;
//    
//    if (![udpSocket bindToPort:0 error:&error])
//    {
//        NSLog(@"Error binding: %@", error);
//        return;
//    }
//    if (![udpSocket beginReceiving:&error])
//    {
//        NSLog(@"Error receiving: %@", error);
//        return;
//    }
//    
//    NSLog(@"Ready");


}


- (IBAction)sendAction:(id)sender {
    
    
    _receiveMsgLabel.text=@"";
    if (_ipText.text.length>0&&_portText.text.length>0&&_msgText.text.length>0) {
        
        [self showIndicatorView:@"发送中"];
        [self sendMsg:_msgText.text host:_ipText.text port:_portText.text];
    }
    
    
    
    else{
        
        [self showAlertView:@"请输入必要信息"];
        
    }
    

    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)sendMsg:(NSString * )Msg host:(NSString *) host port:(NSString *) port
{
    
    NSData *data = [Msg dataUsingEncoding:NSUTF8StringEncoding];
    [udpSocket sendData:data toHost:host port:port.intValue withTimeout:-1 tag:1];
    
    
    
}

- (void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag
{
            [self dismissIndicatorView];
      NSLog(@"发送成功");
    // You could add checks here
}

- (void)onUdpSocket:(AsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error
{
    
            [self dismissIndicatorView];
     NSLog(@"未发送成功");
    // You could add checks here
}

- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock
     didReceiveData:(NSData *)data
            withTag:(long)tag
           fromHost:(NSString *)host
               port:(UInt16)port
{
    NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if (msg)
    {
        [self dismissIndicatorView];
        
        NSLog(@"RECV: %@", msg);
    }
    else
    {
        [self dismissIndicatorView];

        NSLog(@"RECV: RECV: Unknown message from: %@:%hu", host, port);
        
        
        
    }
    
    [udpSocket receiveWithTimeout:-1 tag:0];
    return YES;
}

@end
